package com.jvbc.xoso.database;

import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
import com.jvbc.xoso.bean.*;

public class KetQuaDataSource {
	static final String URL_JSON_KET_QUA_BY_DATE_TINH="http://192.168.1.102/xosocms/xemketqua/theotinh/a";
	static final String URL_JSON_THONG_KE="http://192.168.1.102/xosocms/thongke/capcuoi/a";
	
	public static List<CMSKetQua> GetCMSKetQuaByDateTinh(String date,String maTinh){
		List<CMSKetQua> list=new ArrayList<CMSKetQua>();
		String url=URL_JSON_KET_QUA_BY_DATE_TINH+"?date="+date+"&ma="+maTinh+"&total=1";
		String jsonStr=JSONfunctions.getJSONStringfromURL(url);
		try {
			JSONArray jsonarray=new JSONArray(jsonStr);
			JSONObject jsonobject;
			CMSKetQua item = null;
			for (int i = 0; i < jsonarray.length(); i++) {
				item=new CMSKetQua();
				jsonobject = jsonarray.getJSONObject(i);
				item.setGiaiDb(jsonobject.getString("giaiDb"));
				item.setGiaiNhat(jsonobject.getString("giaiNhat"));
				item.setGiaiNhi(jsonobject.getString("giaiNhi"));
				item.setGiaiBa(jsonobject.getString("giaiBa"));
				item.setGiaiTu(jsonobject.getString("giaiTu"));
				item.setGiaiNam(jsonobject.getString("giaiNam"));
				item.setGiaiSau(jsonobject.getString("giaiSau"));
				item.setGiaiSau(jsonobject.getString("giaiBay"));	
				item.setGiaiSau(jsonobject.getString("giaiTam"));
				item.setCapCuoi(jsonobject.getString("capCuoi"));
				item.setDau0(jsonobject.getString("dau0"));
				item.setDau1(jsonobject.getString("dau1"));
				item.setDau2(jsonobject.getString("dau2"));
				item.setDau3(jsonobject.getString("dau3"));
				item.setDau4(jsonobject.getString("dau4"));
				item.setDau5(jsonobject.getString("dau5"));
				item.setDau6(jsonobject.getString("dau6"));
				item.setDau7(jsonobject.getString("dau7"));
				item.setDau8(jsonobject.getString("dau8"));
				item.setDau9(jsonobject.getString("dau9"));
				item.setCuoi0(jsonobject.getString("cuoi0"));
				item.setCuoi1(jsonobject.getString("cuoi1"));
				item.setCuoi2(jsonobject.getString("cuoi2"));
				item.setCuoi3(jsonobject.getString("cuoi3"));
				item.setCuoi4(jsonobject.getString("cuoi4"));
				item.setCuoi5(jsonobject.getString("cuoi5"));
				item.setCuoi6(jsonobject.getString("cuoi6"));
				item.setCuoi7(jsonobject.getString("cuoi7"));
				item.setCuoi8(jsonobject.getString("cuoi8"));
				item.setCuoi9(jsonobject.getString("cuoi9"));
				list.add(item);
			}
		} catch (JSONException e) {
			Log.e("Error", e.getMessage());
			e.printStackTrace();
		}
		return list;
	}
	
	public static List<CMSThongKe> GetCMSThongKe(String maTinh,int total){
		List<CMSThongKe> list=new ArrayList<CMSThongKe>();
		String url=URL_JSON_THONG_KE+"?total="+total+"&ma="+maTinh;
		String jsonStr=JSONfunctions.getJSONStringfromURL(url);
		try {
		
			JSONArray jsonarray=new JSONArray(jsonStr);
			JSONObject jsonobject;
			CMSThongKe item = null;
			for (int i = 0; i < jsonarray.length(); i++) {
				item=new CMSThongKe();
				jsonobject = jsonarray.getJSONObject(i);
				item.setId(jsonobject.getString("id"));
				item.setPhanTram(jsonobject.getDouble("phanTram"));
				item.setTongSo(jsonobject.getInt("tongSo"));
				list.add(item);
			}
		} catch (JSONException e) {
			Log.e("Error", e.getMessage());
			e.printStackTrace();
		}
		return list;
	}
}

